/**
 * 
 */
package com.xml;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author TieSheng 2010-12-21 10:16:30
 */
public class AnalyzeXmlDom4j {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		String path = "d:/flag.xml";
		AnalyzeXmlDom4j t = new AnalyzeXmlDom4j();
		t.test(path);
	}

	@SuppressWarnings("rawtypes")
	public void test(String path) throws Exception{
		File inputXml = new File(path);
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(inputXml);
		Element root = document.getRootElement();
		
		Element node = null;
		for(Iterator i = root.elementIterator(); i.hasNext();) {
			Element t = (Element) i.next();
			if(t.getName().equals("TreeModel")){
				for(Iterator j = t.elementIterator(); j.hasNext();) {
					Element m = (Element) j.next();
					if(m.getName().equals("Node")){
						node = m;
						break;
					}
				}
				break;
			}
		}
		
		if(node!=null){
			format(node);
		}
	}
	
	@SuppressWarnings("rawtypes")
	private void format(Element root){
		System.out.println(root.valueOf("@id"));
		
		for(Iterator j = root.elementIterator(); j.hasNext();) {
			Element m = (Element) j.next();
			if(!m.getName().equals("Node")){
				continue;
			}

			format(m);
		}
	}
	
}
